//
//  SEGTarget_Analytics.m
//  Commom
//
//  Created by 山刘 on 2019/4/16.
//  Copyright © 2019年 山刘. All rights reserved.
//

#import "SEGTarget_Analytics.h"
#import "SEGIAnalyticsManager.h"

#if __has_include(<Commom/Aspects.h>)
#import <Commom/Aspects.h>
#else
#import "Aspects.h"
#endif

@implementation SEGTarget_Analytics
/**
 设置开始进行页面埋点记录
 */
-(void)segAction_startAnalytics:(NSDictionary*)params{
    [UIViewController aspect_hookSelector:@selector(viewWillAppear:)
                              withOptions:AspectPositionBefore
                               usingBlock:^(id<AspectInfo> info) {
                                   [[SEGIAnalyticsManager sharedAnalyticsManager]segBeginLogPageView:NSStringFromClass([[info instance] class])];
                                   
                                   if(@available(iOS 11.0, *)){
                                       if ([[info instance] isKindOfClass:[UIViewController class]]) {
                                           UIViewController *VC = [info instance];
                                           [self seg_traverseAllViews:VC.view.subviews viewControllor:VC];
                                       }
                                   }
                               } error:NULL];
    
    [UIViewController aspect_hookSelector:@selector(viewWillDisappear:)
                              withOptions:AspectPositionBefore
                               usingBlock:^(id<AspectInfo> info) {
                                   [[SEGIAnalyticsManager sharedAnalyticsManager]segEndLogPageView:NSStringFromClass([[info instance] class])];
                               } error:NULL];
}

// !!!:  Sam，2017-09-25, iOS11系统适配
-(void)seg_traverseAllViews:(NSArray*)dataSource viewControllor:(UIViewController*)VC{
    
    for (UIView* object in dataSource) {
        
        if ([object isKindOfClass:[UITableView class]]) {
            UITableView *tableView = (UITableView*)object;
            if(@available(iOS 11.0, *)){
                
                tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
                tableView.estimatedRowHeight  = tableView.estimatedRowHeight > 0 ? tableView.estimatedRowHeight : 0;
                tableView.estimatedSectionHeaderHeight  = tableView.estimatedSectionHeaderHeight > 0 ? tableView.estimatedSectionHeaderHeight : 0;
                tableView.estimatedSectionFooterHeight  = tableView.estimatedSectionFooterHeight > 0 ? tableView.estimatedSectionFooterHeight : 0;
            }else{
                VC.automaticallyAdjustsScrollViewInsets = VC.automaticallyAdjustsScrollViewInsets ? VC.automaticallyAdjustsScrollViewInsets : NO;
            }
        }else if ([object isKindOfClass:[UIScrollView class]]){
            if (@available(iOS 11, *)) {
                [UIScrollView appearance].contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
            }
        }
    }
}
@end
